package net.maxwoods.nimble;

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.Union;
import com.sun.jna.platform.win32.WinBase;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.win32.StdCallLibrary;

import java.util.List;

public interface BlueTools extends StdCallLibrary {

    class BLUETOOTH_FIND_RADIO_PARAMS extends Structure {

        public WinDef.DWORD dwSize;

        public static class ByReference extends BLUETOOTH_FIND_RADIO_PARAMS implements Structure.ByReference {
        }

        public static class ByValue extends BLUETOOTH_FIND_RADIO_PARAMS implements Structure.ByValue {

        }

        public static final List<String> FIELDS = createFieldsOrder(new String[]{"dwSize"});

        protected List<String> getFieldOrder() {
            return FIELDS;
        }
    }

    class HBLUETOOTH_RADIO_FIND extends WinNT.HANDLE {

    }

    class HBLUETOOTH_DEVICE_FIND extends WinNT.HANDLE {

    }

    class BLUETOOTH_ADDRESS extends Union {

        public static class ByReference extends BLUETOOTH_ADDRESS implements Structure.ByReference {
        }

        public static class ByValue extends BLUETOOTH_ADDRESS implements Structure.ByValue {

        }

        public WinDef.ULONGLONG ullLong;
        public WinDef.BYTE rgBytes[] = new WinDef.BYTE[6];
        public static final List<String> FIELDS = createFieldsOrder(new String[]{"ullLong", "rgBytes"});

        protected List<String> getFieldOrder() {
            return FIELDS;
        }
    }

    class BLUETOOTH_RADIO_INFO extends Structure {
        public WinDef.DWORD dwSize;
        public BLUETOOTH_ADDRESS address;
        public char szName[] = new char[248];
        public WinDef.ULONG ulClassofDevice;
        public WinDef.USHORT lmpSubversion;
        public WinDef.USHORT manufacturer;
        public static final List<String> FIELDS = createFieldsOrder(new String[]{"dwSize", "address", "szName",
                "ulClassofDevice", "lmpSubversion", "manufacturer"});

        protected List<String> getFieldOrder() {
            return FIELDS;
        }
    }


    class BLUETOOTH_DEVICE_SEARCH_PARAMS extends Structure {

        public WinDef.DWORD dwSize;
        public WinDef.BOOL fReturnAuthenticated;
        public WinDef.BOOL fReturnRemembered;
        public WinDef.BOOL fReturnUnknown;
        public WinDef.BOOL fReturnConnected;
        public WinDef.BOOL fIssueInquiry;
        public WinDef.UCHAR cTimeoutMultiplier;
        public WinNT.HANDLE hRadio;

        public static class ByReference extends BLUETOOTH_DEVICE_SEARCH_PARAMS implements Structure.ByReference {
        }

        public static class ByValue extends BLUETOOTH_DEVICE_SEARCH_PARAMS implements Structure.ByValue {

        }

        public static final List<String> FIELDS = createFieldsOrder(new String[]{"dwSize", "fReturnAuthenticated", "fReturnRemembered",
                "fReturnUnknown", "fReturnConnected", "fIssueInquiry", "cTimeoutMultiplier", "hRadio"});

        protected List<String> getFieldOrder() {
            return FIELDS;
        }
    }

    public class BLUETOOTH_DEVICE_INFO extends Structure {
        public WinDef.DWORD dwSize;
        public BLUETOOTH_ADDRESS Address;
        public WinDef.ULONG ulClassofDevice;
        public WinDef.BOOL fConnected;
        public WinDef.BOOL fRemembered;
        public WinDef.BOOL fAuthenticated;
        public WinBase.SYSTEMTIME stLastSeen;
        public WinBase.SYSTEMTIME stLastUsed;
        public WinDef.CHAR szName[] = new WinDef.CHAR[248];

        public static class ByReference extends BLUETOOTH_DEVICE_INFO implements Structure.ByReference {
        }

        public static class ByValue extends BLUETOOTH_DEVICE_INFO implements Structure.ByValue {

        }

        public static final List<String> FIELDS = createFieldsOrder(new String[]{"dwSize", "Address", "ulClassofDevice",
                "fConnected", "fRemembered", "fAuthenticated", "stLastSeen", "stLastUsed", "szName"});

        protected List<String> getFieldOrder() {
            return FIELDS;
        }
    }


    BlueTools INSTANCE = (BlueTools) Native.loadLibrary("irprops", BlueTools.class);

    /**查找第一个蓝牙无线电*/
    HBLUETOOTH_RADIO_FIND BluetoothFindFirstRadio(BLUETOOTH_FIND_RADIO_PARAMS pbtfrp, WinNT.HANDLEByReference phRadio);

    /**查找下一个蓝牙无线电*/
    WinDef.BOOL BluetoothFindNextRadio(HBLUETOOTH_RADIO_FIND hbluetooth_radio_find, WinNT.HANDLEByReference phRadio);

    /**获取蓝牙设无线电信息*/
    WinDef.DWORD BluetoothGetRadioInfo(WinNT.HANDLE hRadio, BLUETOOTH_RADIO_INFO pRadioInfo);

    /**查找第一个蓝牙设备*/
    HBLUETOOTH_DEVICE_FIND BluetoothFindFirstDevice(BLUETOOTH_DEVICE_SEARCH_PARAMS btsp, BLUETOOTH_DEVICE_INFO.ByReference btdi);

    /**查找下一个蓝牙设备*/
    WinDef.BOOL BluetoothFindNextDevice(HBLUETOOTH_DEVICE_FIND hFind, BLUETOOTH_DEVICE_INFO.ByReference btdi);
}
